package com.yuju.rpt.service.impl;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yuju.account.service.IAccountService;
import com.yuju.enums.ReturnCodeEnum;
import com.yuju.exception.BusinessException;
import com.yuju.order.model.ReserveModel;
import com.yuju.order.model.RoomOrderModel;
import com.yuju.rpt.dao.IRptOrderDao;
import com.yuju.rpt.service.IRptOrderService;
import com.yuju.util.ExcelExp;
import com.yuju.util.Format;
import com.yuju.util.Utils;

/**
 * 
 * 类名：OrderRptServiceImpl  <br />
 *
 * 功能：订单报表
 *
 * @author : xuehj <br />
 * @Date : 2018年1月24日 下午1:54:33  <br />
 * @version : 1.1 <br />
 */
@Service
public class RptOrderServiceImpl implements IRptOrderService {
	@Autowired
	private IAccountService accountService;
	
	@Autowired
	private IRptOrderDao rptOrderDao;
	
	@Override
	public byte[] expCheckoutRpt(RoomOrderModel model) throws IOException {
		ExcelExp e = new ExcelExp();
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		e.createRow(0);
		
		e.setCell(0, "房号");
		e.setCell(1, "房型");
		e.setCell(2, "房价");
		e.setCell(3, "姓名");
		e.setCell(4, "证件号码");
		e.setCell(5, "入住时间");
		e.setCell(6, "预离时间");
		e.setCell(7, "消费金额");
		e.setCell(8, "结算金额");
		e.setCell(9, "余额");
		e.setCell(10, "在住状态");
		
		//model.setCheckoutTime(model.getdu);
		List<RoomOrderModel> list = rptOrderDao.checkoutList(model);
		if (Utils.isNull(list) || list.isEmpty()) {
			throw new BusinessException(ReturnCodeEnum.NO_DATA.getCode(), ReturnCodeEnum.NO_DATA.getDesc());
		}
		
		
		//根据订单号获取总消费和总应收
		for (int i = 0; i < list.size(); i++) {
			Map<String, Object> detailByOrderId = accountService.getDetailByOrderId(list.get(i).getId());
			//总消费
			list.get(i).setTotalBill(detailByOrderId.get("ysSum"));
			//总收款
			list.get(i).setSettlementTotal(detailByOrderId.get("ssSum"));
		}
		
		for(int i=0; i<list.size(); i++) {
			double totalBill = 0;
			double settlementTotal = 0;
			Object val = list.get(i).getTotalBill();
			if (val instanceof BigDecimal) {
				totalBill = ((BigDecimal) val).doubleValue();
			}
			
			val = list.get(i).getSettlementTotal();
			if (val instanceof BigDecimal) {
				settlementTotal = ((BigDecimal) val).doubleValue();
			}
			
			e.createRow(i+1);
			e.setCell(0, list.get(i).getRoomCode());
			e.setCell(1, list.get(i).getRoomTypeName());
			e.setCell(2, list.get(i).getRoomPrice().doubleValue());
			e.setCell(3, list.get(i).getName());
			e.setCell(4, list.get(i).getCertificateCode());
			e.setCell(5, Format.getDateStr(list.get(i).getCheckinTime()));
			e.setCell(6, Format.getDateStr(list.get(i).getCheckoutTime()));
			e.setCell(7, totalBill);
			e.setCell(8, settlementTotal);
			e.setCell(9, list.get(i).getBalance().doubleValue());
			e.setCell(10, list.get(i).getCheckinStatusName());
		}
		e.getWorkbook().write(os);
		os.flush();
		return os.toByteArray();
	}
	
	@Override
	public byte[] expCheckinRpt(ReserveModel model) throws IOException {
		ExcelExp e = new ExcelExp();
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		e.createRow(0);
		
		e.setCell(0, "预订人");
		e.setCell(1, "预订方式");
		e.setCell(2, "手机号码");
		e.setCell(3, "预订时间");
		e.setCell(4, "预抵时间");
		e.setCell(5, "天数");
		e.setCell(6, "间数");
		e.setCell(7, "预订金");
		e.setCell(8, "状态");
		
		e.setCell(9, "证件号");
		e.setCell(10, "预订单号");
		
		//model.setCheckoutTime(model.getdu);
		List<ReserveModel> list = rptOrderDao.checkinList(model);
		if (Utils.isNull(list) || list.isEmpty()) {
			throw new BusinessException(ReturnCodeEnum.NO_DATA.getCode(), ReturnCodeEnum.NO_DATA.getDesc());
		}
				
		for(int i=0; i<list.size(); i++) {
			e.createRow(i+1);
			e.setCell(0, list.get(i).getReserveName());
			e.setCell(1, list.get(i).getChannelName());
			e.setCell(2, list.get(i).getMobile());
			e.setCell(3, Format.getDateStr(list.get(i).getGmtCreate()));
			e.setCell(4, Format.getDateStr(list.get(i).getInTime()));
			e.setCell(5, list.get(i).getDayCount());
			e.setCell(6, list.get(i).getRoomCount());
			e.setCell(7, list.get(i).getReserveMoney().doubleValue());
			e.setCell(8, list.get(i).getReserveStatusName());
			e.setCell(9, list.get(i).getCertificateCode());
			e.setCell(10, list.get(i).getReserveNo());
		}
		e.getWorkbook().write(os);
		os.flush();
		return os.toByteArray();
	}
}
